<template>
  <view class="page">
    <!--		试卷分类选择-->
    <view class="tab-list">
      <view
        class="tab-item"
        v-for="item in paperClassList"
        :key="item.classId"
        :class="{ active: currentSelectPaperClassId === item.classId }"
        @click="handleTabClick(item)"
      >
        <text>{{ item.className }}</text>
        <view class="point" v-show="currentSelectPaperClassId === item.classId"></view>
      </view>
    </view>
    <view class="topic-class-list" v-if="topicClassList.length > 0">
      <view class="topic-class-item" v-for="item in topicClassList" :key="item.typeCode" @click="() => viewDetails(item)">
        <view>
          <view class="typeName part">{{ item.typeName }}</view>
          <view class="accuracy part">错误率：{{ item.accuracy }}</view>
        </view>
        <!--					右箭头-->
        <image :src="rightArrowIcon" class="right-icon"></image>
      </view>
    </view>
    <empty v-else height="calc(100vh - 120rpx)"></empty>
  </view>
</template>

<script>
import websocket from "@/utils/websocket"
import request from "@/utils/request"
import empty from "@/components/empty.vue"
import rightArrowIcon from "./assets/right-arrow.png"
import { handleUrlParams, valIsEmpty } from "@/utils"
export default {
  data() {
    return {
      topicClassList: [],
      paperClassList: [], //试卷分类列表
      currentSelectPaperClassId: "", //当前选中的试卷分类
      rightArrowIcon,
    }
  },
  components: { empty },
  onLoad(options) {
		if (options.classId) {
			this.currentSelectPaperClassId = Number.parseInt(options.classId)
		}
    this.getPaperClassList()
  },
  methods: {
    // tab切换
    handleTabClick(item) {
      this.currentSelectPaperClassId = item.classId
      this.currentSelectPaperClassName = item.className
      this.getTopicClassList()
    },
    //获取试卷分类
    getPaperClassList() {
      request({
        url: "topicClass/page",
        data: {
          current: 1,
          size: -1,
        },
        showLoading: true,
        errorHandle: true,
      }).then((data) => {
        this.paperClassList = data.records
        //默认选中第一个分类
				if (valIsEmpty(this.currentSelectPaperClassId)) {
					this.currentSelectPaperClassId = data.records[0].classId
				}
        this.getTopicClassList()
      })
    },
    //获取题型列表
    getTopicClassList() {
      request({
        url: "brushQuestions/accuracy",
        data: {
          classId: this.currentSelectPaperClassId,
        },
        errorHandle: true,
        showLoading: true,
      })
        .then((data) => {
          this.topicClassList = data
        })
        .catch((err) => {
          this.topicClassList = []
        })
    },
    //获取题型题目列表
    viewDetails(item) {
      uni.navigateTo({
        url: handleUrlParams("/mine/pages/wrongTopicList/topicList", {
          typeCode: item.typeCode,
          typeName: item.typeName,
          classId: this.currentSelectPaperClassId,
          className: this.currentSelectPaperClassName,
        }),
      })
    },
  },
  onShow() {
    websocket.trackCurrentPage("错题合集").catch(() => {})
  },
}
</script>

<style lang="scss" scoped>
.page {
  min-height: 100vh;
  background: #fffcf6;
  .tab-list {
    display: flex;
    align-content: center;
    width: 100vw;
    padding: {
      top: 20rpx;
      left: 10rpx;
      right: 10rpx;
    }
    overflow-x: auto;
    .tab-item {
      font-size: 30rpx;
      margin-left: 30rpx;
      position: relative;
      padding-bottom: 20rpx;
      min-width: max-content;
      &.active {
        color: var(--text-color-theme);
      }
      .point {
        position: absolute;
        bottom: 6rpx;
        height: 8rpx;
        width: 30%;
        left: 50%;
        transform: translateX(-50%);
        background: var(--theme-color);
        border-radius: 4rpx;
      }
    }
  }
  .topic-class-list {
    padding: 0 30rpx;
    .topic-class-item {
			box-shadow: 0 0 20rpx 0 rgba(0, 0, 0, 0.1);
      background: var(--background-color);
      border-radius: 20rpx;
      margin-top: 30rpx;
      padding: 30rpx;
      display: flex;
      align-items: center;
      justify-content: space-between;
      .typeName {
        font-size: 34rpx;
        font-weight: bold;
      }
      .accuracy {
        font-size: 30rpx;
        color: var(--text-color-light);
        margin-top: 20rpx;
      }
      .right-icon {
        width: 30rpx;
        height: 30rpx;
      }
    }
  }
}
</style>
